<template>
  <el-dialog
    v-model="dialogVisible"
    :title="dialogTitle"
    width="36%"
    @close="onClose"
  >
    <slot name="content"></slot>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="onCancel">取消</el-button>
        <el-button
          type="primary"
          @click="onSubmit"
        >
          确定
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { FormMode, FormTitleByMode } from '@/common/type/baseEnum'

const dialogTitle = ref('')
// 表单模式，note：初始化形式
const formMode = ref<FormMode>(FormMode.Add)

const dialogVisible = ref(false)
const open = (m: FormMode) => {
  formMode.value = m // 修改为响应式赋值
  dialogTitle.value = FormTitleByMode[m]
  dialogVisible.value = true
}

const close = () => {
  dialogVisible.value = false
}

// 表单操作
const onClose = () => {
  emit('onClose')
}

const onCancel = () => {
  emit('onCancel')
  close()
}

const onSubmit = () => {
  emit('onSubmit')
}
// note：注意这里的父子显示传递
defineExpose({
  open,
  close
})

const emit = defineEmits(['onSubmit', 'onClose', 'onCancel'])

</script>
